﻿//**********************************************************************************************//
//                                                                                              //
//Author:       Ja'Michael Garcia                                                               //
//Class Name:   AudioManager.cs                                                                 //
//Purpose:      Controls the audio being played throughout the lifetime of the game.            //
//Date :        December 2012                                                                   //
//                                                                                              //
//**********************************************************************************************//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Media;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Audio;

namespace Indie_Game_Contest
{
    class AudioManager : Microsoft.Xna.Framework.Game
    {

        //////////////////////////////////////////////////////////////////////////
        // Data Members
        static AudioManager instance;
        public static AudioManager Instance
        {
            get
            {
                if (instance == null)
                    instance = new AudioManager();

                return instance;
            }
        }

        //////////////////////////////////////////////////////////////////////////



        public void AddSong( string szName, Song song)
        {
            if (SongBank.ContainsValue( song ) == false )
                SongBank.Add(szName, song);
        }

        public void AddSFX(string szName, AudioClip sfx)
        {
            if (SfxBank.ContainsValue( sfx ) == false )
                SfxBank.Add(szName, sfx);
        }

        public void PlaySFX(string szName )
        {
            if (SfxBank.ContainsKey( szName ) == true)
                SfxBank[szName].AudioController.Play();
        }

        public void PauseSFX(string szName)
        {
            if (SfxBank.ContainsKey(szName) == true)
                SfxBank[szName].AudioController.Pause();
        }

        public void StopSFX(string szName)
        {
            if (SfxBank.ContainsKey(szName) == true)
                SfxBank[szName].AudioController.Stop();
        }

        public void PlaySong(string szName, bool bLoopSong = false)
        {
            MediaPlayer.IsRepeating = bLoopSong;
            if (MediaPlayer.State == MediaState.Playing)
            {
                MediaPlayer.Stop();
                MediaPlayer.Play(SongBank[szName]);
            }
            else if (MediaPlayer.State == MediaState.Paused)
                MediaPlayer.Resume();
            else
                MediaPlayer.Play(SongBank[szName]);
        }

        public void PauseSong(string szName)
        {
            if (MediaPlayer.State == MediaState.Playing)
                MediaPlayer.Pause();
            else
                return;
        }

        public void StopSong(string szName)
        {
            if (MediaPlayer.State == MediaState.Playing ||
               MediaPlayer.State == MediaState.Paused )
                MediaPlayer.Stop();
            else
                return;

            //MediaPlayer.res
        }

        public void AdjustSongVolume(float fVol)
        {
            if (fVol > 1.0f)
                fVol = 1.0f;
            else if (fVol < 0.0f)
                fVol = 0.0f;
            MediaPlayer.Volume = fVol;
        }

        public void AdjustSFXVolume(float fVol)
        {
            if (fVol > 1.0f)
                fVol = 1.0f;
            else if (fVol < 0.0f)
                fVol = 0.0f;

            for (int i = 0; i < SfxBank.Count; ++i)
            {
                SfxBank.ElementAt(i).Value.FVolume = fVol;
                SfxBank.ElementAt(i).Value.AudioController.Volume = SfxBank.ElementAt(i).Value.FVolume;
            }
        }

        public void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
            MediaPlayer.Stop();
            //for (int i = 0; i < SfxBank.Count; ++i)
                //SfxBank.ElementAt(i).Value.AudioController.Stop();
            SongBank.Clear();
            SfxBank.Clear();
        }

        private Dictionary<string, Song> songBank = new Dictionary<string, Song>();
       
        private Dictionary<string, Song> SongBank
        {
            get { return songBank; }
            set { songBank = value; }
        }
        private Dictionary<string, AudioClip> sfxBank = new Dictionary<string, AudioClip>();

        private Dictionary<string, AudioClip> SfxBank
        {
            get { return sfxBank; }
            set { sfxBank = value; }
        }
    }
}
